package com.gis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.entity.BenthosData;
import com.gis.entity.SedimentData;
import com.gis.result.ResponseResult;
import com.gis.service.BenthosDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequiredArgsConstructor
@RequestMapping("/benthos_data")
@Tag(name = "底栖生物", description = "底栖生物接口")
@CrossOrigin(origins = "*")
public class BenthosDataController {
    @Autowired
    private BenthosDataService benthosDataService;

    @PostMapping("/addBatch")
    @Operation(summary = "批量添加底栖生物数据")
    public ResponseResult<String> AddBenthosDataServeice(@RequestBody List<BenthosData> benthosDataList) {
        benthosDataService.saveBatch(benthosDataList);
        // benthosDataService.save(benthosData);
        return ResponseResult.success("insert successfully");
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有底栖生物数据")
    public ResponseResult<List<BenthosData>> GetAllBenthosData() {
        List<BenthosData> benthosDatas = benthosDataService.list();
        return ResponseResult.success(benthosDatas);
    }
    @GetMapping("/getByTime")
    @Operation(summary = "根据年份和季度获取底栖生物数据")
    public ResponseResult<List<BenthosData>> GetByTime(@RequestParam Integer year, @RequestParam String season) {
        QueryWrapper<BenthosData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year).eq("season", season);
        List<BenthosData> roeDatas = benthosDataService.list(queryWrapper);
        return ResponseResult.success(roeDatas);
    }

    @Operation(summary = "更新底栖生物数据")
    @PostMapping("/update")
    @Parameters({
        @Parameter(name = "benthosData", description = "底栖生物数据", required = true)
    })
    public ResponseResult<BenthosData> UpdateBenthosData(@RequestBody BenthosData benthosData) {
        benthosDataService.saveOrUpdate(benthosData);
        return ResponseResult.success(benthosData);
    }

    @Operation(summary = "根据ID删除底栖生物数据")
    @PostMapping("/deleteById")
    @Parameters({
        @Parameter(name = "id", description = "底栖生物数据ID", required = true)
    })
    public ResponseResult<String> DeleteBenthosDataById(@RequestBody Object json) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(json));
        Integer id = jsonObject.getInteger("id");
        benthosDataService.removeById(id);
        return ResponseResult.success("delete successfully");
    }

    @Operation(summary = "分页获取底栖生物数据")
    @GetMapping("/listPage")
    @Parameters({
        @Parameter(name = "pageNo", description = "页码", required = true),
        @Parameter(name = "pagesize", description = "每页数量", required = true)
    })
    public ResponseResult<IPage<BenthosData>> ListPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        IPage<BenthosData> page = new Page<>(pageNo, pageSize, true);
        return ResponseResult.success(benthosDataService.page(page));
    }

}
